package com.ccit.springbootlaw.controller.base;

import com.ccit.springbootlaw.controller.resful.Code;
import com.ccit.springbootlaw.controller.resful.Result;
import com.ccit.springbootlaw.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/api/files")
public class FileHandlerController {

    /**
     * 保存文件至阿里OSS服务
     *
     * @param file 要保存的文件对象
     * @return 返回保存文件的结果
     */
    @PostMapping("/save")
    public Result saveFile(MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = AliOssUtil.uploadFile(filename, file.getInputStream());
        return new Result(Code.SAVE_OK, url, "文件上传成功");
    }
}